// pages/workLog/curMonth.js
const app = getApp();
const network = require("../../utils/network.js");
const util = require("../../utils/util.js");
const {multiply} = require("../../utils/amount");
Page({
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多 slot 支持
    },
    /**
     * 页面的初始数据
     */
    data: {
        useColor: util.themeColor,
        type: 1, // 1 公司  2 个人
        monthData: {},
        listData: [],
        source_data: [],
        requestUrl: app.api.customerMy,
        requestParams: {isMock: false},
        customer_id: -1,
        name: ''
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onLoad() {
        this.getData();
    },

    onTabChange({detail: {index, name, title}}) {
        const _this = this
        if (this.type !== name) {
            this.setData({
                name: ''
            })
        }
        this.setData({
            type: name
        }, function () {
            _this.getData()
        })
    },

    showAddRecordDialog(e) {
        this.setData({
            customer_id: e.target.dataset.item.id
        })
        this.selectComponent("#AddFollowRecordDialog").showDialog()
    },

    getData() {
        util.showLoading()
        const _this = this
        if (this.data.type === 1 || this.data.type === 2) {
            network.GET({
                uri: app.api.saleMonthCur,
                params: {
                    type: _this.data.type
                },
                success(res) {
                    _this.setData({
                        monthData: res.data
                    })
                    util.hideLoading()
                },
                fail() {
                    util.hideLoading()
                }
            })
        } else {
            network.GET({
                uri: app.api.advanceList,
                params: {},
                success(res) {
                    res.data.forEach(item => {
                        item.activeNames = []
                    })
                    _this.setData({
                        listData: res.data,
                        source_data: res.data
                    })
                    util.hideLoading()
                },
                fail() {
                    util.hideLoading()
                }
            })
        }
    },

    toFllowList(e) {
        this.showAddRecordDialog(e);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        // if (this.data.type == 3) {
        //     this.selectComponent('.list-component').handlePullDownRefresh()
        // }
    },
    onSearch(e) {
        this.data.source_data.forEach(item => {
            console.log(item)
        })
        let filter = this.data.source_data.filter(item => (item.name + '').indexOf(e.detail) >= 0);
        console.log(filter)
        this.setData({
            listData: filter
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // if (this.data.type == 3) {
        //     this.selectComponent('.list-component').handleReachBottom()
        // }
    },
    addSuccess() {
        this.setData({
            customer_id: -1
        })
    }
})
